/* Editor.java
 * Created on Sep 7, 2010, 3:40:12 PM
 */
package resultEditor.simpleSchema;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.ListSelectionModel;
import relationship.simple.dataTypes.AttributeSchemaDef;
import resultEditor.annotationClasses.AnnotationClass;

/**
 * This Class will be used to Edit the Attributes within the current schema.
 *
 * @see     env.AttributeList
 * @see     env.Attribute
 * @author  Jianwei Chris Leng
 *
 * Created on Sep 7, 2010, 3:40:12 PM
 * @modified on Jan 3, 2012
 */
public class Editor extends javax.swing.JFrame {
    //<editor-fold defaultstate="collapsed" desc="Member Variables">

    private userInterface.GUI gui;
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Constructor">
    /**
     * Constructor
     * @param caller - the UserInterface.GUI that is creating this editor.
     */
    public Editor(userInterface.GUI caller) {
        gui = caller;
        gui.setEnabled( false );

        initComponents();
        this.setSize(900, 500);
        setDialogLocation();
        preProcess();


    }
    private HashMap<String, Icon> iconTable = new HashMap<String, Icon>();

    private void preProcess() {
        iconTable.clear();
        iconTable.put("public", icon_1.getIcon());
        iconTable.put("class", icon_2.getIcon());
        iconTable.put("attribute", icon_3.getIcon());
        iconTable.put("attributevalue", icon_4.getIcon());

        this.sortButton1.setVisible(false);

        jPanel22.setVisible(false);

        list_of_classes.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list_of_attributeNames.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list_of_attributeValues.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        showClasses();
        this.list_of_classes.setSelectedIndex(0);


        listPublicAttributes();

    }

    //</editor-fold>
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        changeAttributeName = new javax.swing.JDialog();
        jTestField_attributeName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        icon_1 = new javax.swing.JLabel();
        icon_2 = new javax.swing.JLabel();
        icon_3 = new javax.swing.JLabel();
        icon_4 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jPanel15 = new javax.swing.JPanel();
        jPanel21 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jPanel16 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        list_of_classes = new javax.swing.JList();
        jPanel22 = new javax.swing.JPanel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jLabel5 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jPanel14 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jPanel9 = new javax.swing.JPanel();
        jPanel10 = new javax.swing.JPanel();
        jPanel11 = new javax.swing.JPanel();
        removeName = new javax.swing.JButton();
        up = new javax.swing.JButton();
        down = new javax.swing.JButton();
        sortButton1 = new javax.swing.JButton();
        jPanel12 = new javax.swing.JPanel();
        nameText = new javax.swing.JTextField();
        addName = new javax.swing.JButton();
        jPanel13 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        list_of_attributeNames = new javax.swing.JList();
        jPanel_panelOfUMLSOptions = new javax.swing.JPanel();
        jCheckBox_linkCUICode = new javax.swing.JCheckBox();
        jCheckBox_linkCUILABEL = new javax.swing.JCheckBox();
        jPanel3 = new javax.swing.JPanel();
        jPanel8 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        deleteButton2 = new javax.swing.JButton();
        upButton2 = new javax.swing.JButton();
        downButton2 = new javax.swing.JButton();
        sortButton2 = new javax.swing.JButton();
        jPanel7 = new javax.swing.JPanel();
        valueText = new javax.swing.JTextField();
        addValue = new javax.swing.JButton();
        jPanel17 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        list_of_attributeValues = new javax.swing.JList();
        jPanel23 = new javax.swing.JPanel();
        jCheckBox2 = new javax.swing.JCheckBox();

        changeAttributeName.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        changeAttributeName.setAlwaysOnTop(true);
        changeAttributeName.setBackground(new java.awt.Color(238, 238, 239));

        jTestField_attributeName.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N

        jLabel3.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
        jLabel3.setText("Attribute Name:");

        jButton2.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
        jButton2.setText("OK");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton4.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
        jButton4.setText("Cancel");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout changeAttributeNameLayout = new javax.swing.GroupLayout(changeAttributeName.getContentPane());
        changeAttributeName.getContentPane().setLayout(changeAttributeNameLayout);
        changeAttributeNameLayout.setHorizontalGroup(
            changeAttributeNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(changeAttributeNameLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(changeAttributeNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(changeAttributeNameLayout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(5, 5, 5)
                        .addComponent(jTestField_attributeName, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, changeAttributeNameLayout.createSequentialGroup()
                        .addGap(0, 218, Short.MAX_VALUE)
                        .addComponent(jButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        changeAttributeNameLayout.setVerticalGroup(
            changeAttributeNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(changeAttributeNameLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(changeAttributeNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTestField_attributeName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(changeAttributeNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton4))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        icon_1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resultEditor/simpleSchema/public.png"))); // NOI18N
        icon_1.setText("jLabel5");

        icon_2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resultEditor/simpleSchema/class.png"))); // NOI18N
        icon_2.setText("jLabel5");

        icon_3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resultEditor/simpleSchema/attribute.png"))); // NOI18N
        icon_3.setText("jLabel5");

        icon_4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resultEditor/simpleSchema/attributevalue.png"))); // NOI18N
        icon_4.setText("jLabel5");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Attribute Schema Editor");
        setMinimumSize(new java.awt.Dimension(500, 400));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
            public void windowDeactivated(java.awt.event.WindowEvent evt) {
                formWindowDeactivated(evt);
            }
        });

        jPanel1.setLayout(new java.awt.GridLayout(1, 3, 6, 10));

        jPanel15.setLayout(new java.awt.BorderLayout());

        jPanel21.setBackground(new java.awt.Color(102, 153, 255));
        jPanel21.setLayout(new java.awt.BorderLayout());

        jLabel4.setFont(new java.awt.Font("Book Antiqua", 1, 13)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("Public / Private");
        jPanel21.add(jLabel4, java.awt.BorderLayout.CENTER);

        jPanel15.add(jPanel21, java.awt.BorderLayout.PAGE_START);

        jPanel16.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 102, 204), 2));
        jPanel16.setForeground(new java.awt.Color(0, 102, 204));
        jPanel16.setLayout(new java.awt.BorderLayout());

        jScrollPane3.setBorder(null);

        list_of_classes.setFont(new java.awt.Font("American Typewriter", 0, 12)); // NOI18N
        list_of_classes.setToolTipText("");
        list_of_classes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                list_of_classesMouseReleased(evt);
            }
        });
        jScrollPane3.setViewportView(list_of_classes);

        jPanel16.add(jScrollPane3, java.awt.BorderLayout.CENTER);

        jPanel22.setLayout(new java.awt.BorderLayout());

        jCheckBox1.setFont(new java.awt.Font("American Typewriter", 0, 11)); // NOI18N
        jCheckBox1.setSelected(true);
        jCheckBox1.setText("Inherit the public attributes.");
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1ActionPerformed(evt);
            }
        });
        jPanel22.add(jCheckBox1, java.awt.BorderLayout.CENTER);

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resultEditor/simpleSchema/clip.jpeg"))); // NOI18N
        jPanel22.add(jLabel5, java.awt.BorderLayout.EAST);

        jPanel16.add(jPanel22, java.awt.BorderLayout.SOUTH);

        jPanel15.add(jPanel16, java.awt.BorderLayout.CENTER);

        jPanel1.add(jPanel15);

        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel14.setBackground(new java.awt.Color(102, 153, 255));
        jPanel14.setLayout(new java.awt.BorderLayout());

        jLabel2.setFont(new java.awt.Font("Book Antiqua", 1, 13)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Attribute Types");
        jPanel14.add(jLabel2, java.awt.BorderLayout.CENTER);

        jPanel2.add(jPanel14, java.awt.BorderLayout.PAGE_START);

        jPanel9.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 102, 204), 2));
        jPanel9.setLayout(new java.awt.BorderLayout());

        jPanel10.setLayout(new java.awt.GridLayout(2, 0));

        jPanel11.setLayout(new java.awt.GridLayout(1, 4, 1, 0));

        removeName.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
        removeName.setText("Delete");
        removeName.setToolTipText("<html>Remove the attribute that is selected in the list. Any allowable values<br> associated with the annotation will also be deleted.</html>");
        removeName.setEnabled(false);
        removeName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeNameActionPerformed(evt);
            }
        });
        jPanel11.add(removeName);

        up.setText("<-");
        up.setEnabled(false);
        up.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                upActionPerformed(evt);
            }
        });
        jPanel11.add(up);

        down.setText("->");
        down.setEnabled(false);
        down.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                downActionPerformed(evt);
            }
        });
        jPanel11.add(down);

        sortButton1.setText("Sort");
        sortButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sortButton1ActionPerformed(evt);
            }
        });
        jPanel11.add(sortButton1);

        jPanel10.add(jPanel11);

        jPanel12.setLayout(new java.awt.BorderLayout());

        nameText.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
        nameText.setToolTipText("<html>Type the name of a new Attribute here.<br> Add to the list by pressing enter or the 'Add' button.</html>");
        nameText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                nameTextKeyPressed(evt);
            }
        });
        jPanel12.add(nameText, java.awt.BorderLayout.CENTER);

        addName.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
        addName.setText("Add");
        addName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addNameActionPerformed(evt);
            }
        });
        jPanel12.add(addName, java.awt.BorderLayout.LINE_END);

        jPanel10.add(jPanel12);

        jPanel9.add(jPanel10, java.awt.BorderLayout.PAGE_END);

        jPanel13.setLayout(new java.awt.BorderLayout());

        jScrollPane1.setBorder(null);

        list_of_attributeNames.setFont(new java.awt.Font("American Typewriter", 0, 12)); // NOI18N
        list_of_attributeNames.setToolTipText("<html>The list of attributes that are allowed within this schema.</html>");
        list_of_attributeNames.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                list_of_attributeNamesMouseReleased(evt);
            }
        });
        list_of_attributeNames.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                list_of_attributeNamesValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(list_of_attributeNames);

        jPanel13.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel_panelOfUMLSOptions.setLayout(new java.awt.GridLayout(1, 0));

        jCheckBox_linkCUICode.setFont(new java.awt.Font("American Typewriter", 0, 11)); // NOI18N
        jCheckBox_linkCUICode.setText("linkCUICode");
        jCheckBox_linkCUICode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox_linkCUICodeActionPerformed(evt);
            }
        });
        jPanel_panelOfUMLSOptions.add(jCheckBox_linkCUICode);

        jCheckBox_linkCUILABEL.setFont(new java.awt.Font("American Typewriter", 0, 11)); // NOI18N
        jCheckBox_linkCUILABEL.setText("linkCUILabel");
        jCheckBox_linkCUILABEL.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox_linkCUILABELActionPerformed(evt);
            }
        });
        jPanel_panelOfUMLSOptions.add(jCheckBox_linkCUILABEL);

        jPanel13.add(jPanel_panelOfUMLSOptions, java.awt.BorderLayout.SOUTH);

        jPanel9.add(jPanel13, java.awt.BorderLayout.CENTER);

        jPanel2.add(jPanel9, java.awt.BorderLayout.CENTER);

        jPanel1.add(jPanel2);

        jPanel3.setLayout(new java.awt.BorderLayout());

        jPanel8.setBackground(new java.awt.Color(51, 153, 255));
        jPanel8.setLayout(new java.awt.BorderLayout());

        jLabel1.setFont(new java.awt.Font("Book Antiqua", 1, 13)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Allowable Values");
        jPanel8.add(jLabel1, java.awt.BorderLayout.CENTER);

        jPanel3.add(jPanel8, java.awt.BorderLayout.PAGE_START);

        jPanel5.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 102, 204), 2));
        jPanel5.setLayout(new java.awt.BorderLayout());

        jPanel4.setLayout(new java.awt.GridLayout(2, 0));

        jPanel6.setLayout(new java.awt.GridLayout(1, 5, 2, 0));

        deleteButton2.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
        deleteButton2.setText("Delete");
        deleteButton2.setToolTipText("<html>Remove the allowable value that is selected in the list.</html>");
        deleteButton2.setEnabled(false);
        deleteButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButton2ActionPerformed(evt);
            }
        });
        jPanel6.add(deleteButton2);

        upButton2.setText("<-");
        upButton2.setEnabled(false);
        upButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                upButton2ActionPerformed(evt);
            }
        });
        jPanel6.add(upButton2);

        downButton2.setText("->");
        downButton2.setEnabled(false);
        downButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                downButton2ActionPerformed(evt);
            }
        });
        jPanel6.add(downButton2);

        sortButton2.setText("Sort");
        sortButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sortButton2ActionPerformed(evt);
            }
        });
        jPanel6.add(sortButton2);

        jPanel4.add(jPanel6);

        jPanel7.setLayout(new java.awt.BorderLayout());

        valueText.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
        valueText.setToolTipText("<html>Type an allowable value for the currently selected attribute type.<br>  Add to the list by pressing enter or the \"Add\" button.");
        valueText.setEnabled(false);
        valueText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                valueTextKeyPressed(evt);
            }
        });
        jPanel7.add(valueText, java.awt.BorderLayout.CENTER);

        addValue.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
        addValue.setText("Add");
        addValue.setToolTipText("<html>Add a value to the currently selected Attribute</html>");
        addValue.setEnabled(false);
        addValue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addValueActionPerformed(evt);
            }
        });
        jPanel7.add(addValue, java.awt.BorderLayout.LINE_END);

        jPanel4.add(jPanel7);

        jPanel5.add(jPanel4, java.awt.BorderLayout.PAGE_END);

        jPanel17.setBorder(javax.swing.BorderFactory.createMatteBorder(0, 0, 1, 0, new java.awt.Color(0, 102, 153)));
        jPanel17.setLayout(new java.awt.BorderLayout());

        jScrollPane2.setBorder(null);

        list_of_attributeValues.setFont(new java.awt.Font("American Typewriter", 0, 12)); // NOI18N
        list_of_attributeValues.setToolTipText("<html> The list of values that are allowed for the selected attribute.</html>");
        list_of_attributeValues.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                list_of_attributeValuesValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(list_of_attributeValues);

        jPanel17.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jPanel23.setLayout(new java.awt.BorderLayout());

        jCheckBox2.setFont(new java.awt.Font("American Typewriter", 0, 11)); // NOI18N
        jCheckBox2.setText("Default Value");
        jCheckBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox2ActionPerformed(evt);
            }
        });
        jPanel23.add(jCheckBox2, java.awt.BorderLayout.CENTER);

        jPanel17.add(jPanel23, java.awt.BorderLayout.SOUTH);

        jPanel5.add(jPanel17, java.awt.BorderLayout.CENTER);

        jPanel3.add(jPanel5, java.awt.BorderLayout.CENTER);

        jPanel1.add(jPanel3);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //<editor-fold defaultstate="collapsed" desc="Event Handling">
    /**
     * Call this whenever the currently selected attribute is changed.  If an
     * attribute name is selected enable deletion, and enable addition of Attribute
     * values.  If nothing is selected then disable this actions.
     * @param evt
     */
    private void list_of_attributeNamesValueChanged(javax.swing.event.ListSelectionEvent evt)//GEN-FIRST:event_list_of_attributeNamesValueChanged
    {//GEN-HEADEREND:event_list_of_attributeNamesValueChanged

        // return if the component is adjusting
        if (evt.getValueIsAdjusting()) {
            return;
        }
        
        this.jCheckBox_linkCUICode.setSelected( false );
        this.jCheckBox_linkCUILABEL.setSelected( false );

        // reset buttons
        removeName.setEnabled(false); // Disable: deletion of Attributes
        up.setEnabled(false);
        down.setEnabled(false);
        valueText.setEnabled(false); // Disable: allowable value editing
        addValue.setEnabled(false); // Disable: allowable value editing
        jPanel_panelOfUMLSOptions.setVisible( false );
        //Clear out list of attribute values
        list_of_attributeValues.setListData(new Vector());
        

        // get the selection index
        int selectedIndex = this.list_of_attributeNames.getSelectedIndex();

        if (selectedIndex == -1) { // non item is selected in the list of attribtues
            return;
        }



        //Enable deletion of Attributes
        removeName.setEnabled(true);



        //Enable Editing of Allowable Values
        valueText.setEnabled(true);
        addValue.setEnabled(true);
        //Update list of allowable values for this attribute
        AttributeListObj ao;
        try {
            ao = (AttributeListObj) this.list_of_attributeNames.getModel().getElementAt(selectedIndex);
        } catch (Exception ex) {
            ao = null;
        }

        if (ao == null) {
            return;
        }
        
        this.jPanel_panelOfUMLSOptions.setVisible( true );
        
        // set checkbox for UMLS status of this attribute
        //this.jCheckBox_linkCUICode.setSelected(false);
        //this.jCheckBox_linkCUILABEL.setSelected(false);
        if (ao.isCUICodeNLabel()) {
            this.jCheckBox_linkCUICode.setSelected(true);
            this.jCheckBox_linkCUILABEL.setSelected(true);
        } else {
            if (ao.isCUICode()) {
                this.jCheckBox_linkCUICode.setSelected(true);
                this.jCheckBox_linkCUILABEL.setSelected(false);
            }
            if (ao.isCUILabel()) {
                this.jCheckBox_linkCUICode.setSelected(false);
                this.jCheckBox_linkCUILABEL.setSelected(true);
            }
        }

        // only attributes belong to class can be relocated.
        if (!ao.isPublicAttribute()) {
            if (selectedIndex != 0) {
                up.setEnabled(true);
            } else {
                up.setEnabled(false);
            }
            if (selectedIndex == (this.list_of_attributeNames.getModel().getSize() - 1)) {
                down.setEnabled(false);
            } else {
                down.setEnabled(true);
            }
        }

        Status.currentAttribute = ao.getAttribute();
        

        if (ao.isPublicAttribute()) { // if it's in the only public attribute list
            listAttributeValues(ao.getAttribute(), true);
        } else {
            listAttributeValues(ao.getAttribute(), false);
        }


    }//GEN-LAST:event_list_of_attributeNamesValueChanged
    /**
     * When the 'Add' button is pressed under the List of Attribute names, add
     * the current text as a new name.
     * @param evt
     */
    private void addNameActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_addNameActionPerformed
    {//GEN-HEADEREND:event_addNameActionPerformed
        addNewRelationshipFromTextBox();
        config.project.ProjectConf projectconf = new config.project.ProjectConf(
                    env.Parameters.WorkSpace.CurrentProject);
        projectconf.saveConfigure();
    }//GEN-LAST:event_addNameActionPerformed
    /**
     * When the 'Add' button under the list of allowable values is pressed, add the
     * text in the textbox next to it as a new allowable value.
     * @param evt
     */
    private void addValueActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_addValueActionPerformed
    {//GEN-HEADEREND:event_addValueActionPerformed
        addNewAttributeValue();
        config.project.ProjectConf projectconf = new config.project.ProjectConf(
                    env.Parameters.WorkSpace.CurrentProject);
        projectconf.saveConfigure();
    }//GEN-LAST:event_addValueActionPerformed
    /**
     * When the remove button under the Attribute list is pressed remove the currently
     * selected attribute name.
     * @param evt
     */
    private void removeNameActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_removeNameActionPerformed
    {//GEN-HEADEREND:event_removeNameActionPerformed
     int selectedIndex = this.list_of_attributeNames.getSelectedIndex();
        if (selectedIndex == -1) {
            return;
        }


        AttributeListObj ao = null;
        try {
            ao = (AttributeListObj) this.list_of_attributeNames.getModel().getElementAt(selectedIndex);
        } catch (Exception ex) {
            ao = null;
        }

        if (ao == null) {
            return;
        }

        if (ao.getAttribute() != null) {
        }


        removeAttributes( ao );
        
        config.project.ProjectConf projectconf = new config.project.ProjectConf(
                    env.Parameters.WorkSpace.CurrentProject);
        projectconf.saveConfigure();
         
    }//GEN-LAST:event_removeNameActionPerformed

    
    private void removeAttributes( AttributeListObj ao ){
        list_of_attributeValues.setListData(new Vector());
        if (ao.isPublicAttribute()) {
            env.Parameters.AttributeSchemas.Remove(ao.getAttribute());
            listPublicAttributes();
        } else {
            resultEditor.annotationClasses.Depot classDepot 
                    = new resultEditor.annotationClasses.Depot();
            AnnotationClass ac
                    = classDepot.getAnnotatedClass(Status.currentClass);
            
            if (ac != null) {
                ac.removeAttribute(ao.getAttributeName());
            }
            this.listAttributesOf(Status.currentClass);
        }  
    }
    /**
     * Remove the currently selected value from the list of allowable values for the
     * currently selected Attribute.
     * @param evt
     */
    private void deleteButton2ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_deleteButton2ActionPerformed
    {//GEN-HEADEREND:event_deleteButton2ActionPerformed
        //If no attribute is selected return... Should never happen as this button
        //should only be enabled if an attribute is selected
        if (list_of_attributeNames.getSelectedIndex() == -1) {
            return;
        }
        AttributeListObj ao = null;
        try {
            ao = (AttributeListObj) list_of_attributeNames.getSelectedValue();
        } catch (Exception ex) {
            ao = null;
        }

        if (ao == null) {
            return;
        }

        //Get Selected values
        Object[] objs = list_of_attributeValues.getSelectedValues();

        //Return if nothing selected
        if (objs == null || objs.length == 0) {
            return;
        }

        //Remove ALL selcted values
        for (Object obj : objs) {
            try {
                AttributeValueObj avo = (AttributeValueObj) obj;
                
                if (avo == null) {
                    continue;
                }
                ao.getAttribute().getAllowedEntries().remove(avo.getAttributeValue());
                
                if(ao.getAttribute().hasDefaultValue()){
                    String value = avo.getAttributeValue();               
                    if( ao.getAttribute().hasDefaultValue(value) )
                    {
                        ao.getAttribute().removeDefaultValue();
                    }
                }
            } catch (Exception ex) {
            }

        }


        
        
        
        int index1 = list_of_attributeNames.getSelectedIndex();
        int index2 = list_of_attributeValues.getSelectedIndex();        
        
        //listAttributeValues(ao.getAttribute(), ao.isPublicAttribute());
        this.listAttributeNames();
        
        list_of_attributeNames.setSelectedIndex( index1 );
        
        int size = list_of_attributeValues.getModel().getSize();        
        if(size>0){
            if(index2>=size)
                index2 = size - 1;
            list_of_attributeValues.setSelectedIndex( index2 );
        }
        
        config.project.ProjectConf projectconf = new config.project.ProjectConf(
                    env.Parameters.WorkSpace.CurrentProject);
        projectconf.saveConfigure();
    }//GEN-LAST:event_deleteButton2ActionPerformed

    /**
     * If the user types a new line character into the textbox then add the text
     * as a new attribute name.
     * @param evt
     */
    private void nameTextKeyPressed(java.awt.event.KeyEvent evt)//GEN-FIRST:event_nameTextKeyPressed
    {//GEN-HEADEREND:event_nameTextKeyPressed
        if (evt.getKeyChar() == '\n') {
            addNewRelationshipFromTextBox();
        }
    }//GEN-LAST:event_nameTextKeyPressed
    /**
     * If the user types a new line character into the textbox then add the text
     * as a new allowed value for the currently selected attribute.
     * @param evt
     */
    private void valueTextKeyPressed(java.awt.event.KeyEvent evt)//GEN-FIRST:event_valueTextKeyPressed
    {//GEN-HEADEREND:event_valueTextKeyPressed
        if (evt.getKeyChar() == '\n') {
            addNewAttributeValue();
        }
    }//GEN-LAST:event_valueTextKeyPressed
    /**
     * If an attribute value is currently selected enable the remove button, otherwise
     * disable it.
     * @param evt
     */
    private void list_of_attributeValuesValueChanged(javax.swing.event.ListSelectionEvent evt)//GEN-FIRST:event_list_of_attributeValuesValueChanged
    {//GEN-HEADEREND:event_list_of_attributeValuesValueChanged

        if (list_of_attributeValues.getSelectedIndex() == -1) {
            jCheckBox2.setEnabled( false );
            jCheckBox2.setSelected( false );
            return;
        }

        if (list_of_attributeValues.getSelectedIndex() >= 0) {
            deleteButton2.setEnabled(true);
            jCheckBox2.setEnabled( true );
            
            if (list_of_attributeValues.getSelectedIndex() == 0) {
                upButton2.setEnabled(false);
            } else {
                upButton2.setEnabled(true);
            }

            if (list_of_attributeValues.getSelectedIndex() == list_of_attributeValues.getModel().getSize() - 1) {
                downButton2.setEnabled(false);
            } else {
                downButton2.setEnabled(true);
            }
            
            
            // get current selected value
            AttributeValueObj attvalueobj = (AttributeValueObj) list_of_attributeValues.getSelectedValue();
            String value = attvalueobj.getAttributeValue();
            AttributeListObj attribute = (AttributeListObj) this.list_of_attributeNames.getSelectedValue();
            String defaultvalue = attribute.getAttribute().getDefault();
            if( defaultvalue == null   ){
                this.jCheckBox2.setSelected( false);
            }else{
                if( defaultvalue.compareTo(value) == 0 )
                    this.jCheckBox2.setSelected( true );
                else
                    this.jCheckBox2.setSelected( false);
            }

        } else {
            deleteButton2.setEnabled(false);
            upButton2.setEnabled(false);
            downButton2.setEnabled(false);
            jCheckBox2.setEnabled( false );
            jCheckBox2.setSelected( false );
        }
    }//GEN-LAST:event_list_of_attributeValuesValueChanged

    private void list_of_attributeNamesMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_list_of_attributeNamesMouseReleased

        if (evt.getClickCount() != 2) {
            return;
        }

        try {
            
            // make sure we got a item of "Attribute" selected in the list
            int selectedIndex = list_of_attributeNames.getSelectedIndex();
            if (selectedIndex == -1) {
                return;
            }

            if (list_of_attributeNames.getModel() == null) {
                return;
            }

            if (list_of_attributeNames.getModel().getSize() < 1) {
                return;
            }


            // get the attribute
            AttributeListObj ao = (AttributeListObj) list_of_attributeNames.getSelectedValue();
            if (ao == null) {
                return;
            }

            AttributeSchemaDef att = ao.getAttribute();
            if (att == null) {
                return;
            }

            popupDialog_toChangeAttributeName(ao, att.getName(), selectedIndex);

        } catch (Exception ex) {
        }


    }//GEN-LAST:event_list_of_attributeNamesMouseReleased
    /**This integer is used to remember the real index in the array where we
     * store current selected attribute. Value smaller than 0 indicates that
     * there is no attribute got selected.
     */
    private int selectedAttribute = -1;
    private String oldAttributeName = null;

    /**This will popup a new dialog to let you change the name of the selected
     * attribute item.
     *
     * @param   _attributeName
     *          The name of current selected attribute item in the schema dialog
     *
     * @param   _index
     *          the position of this selected attributes, the index is its real
     *          index in the arrtibute depot
     */
    private void popupDialog_toChangeAttributeName(AttributeListObj attributeObj, String _attributeName, int _index) {

        this.changeAttributeName.setVisible(false);
        selectedAttribute = _index;

        // designate size to this dialog
        final int width = 413, height = 104;
        this.changeAttributeName.setSize(width, height);

        // put this dialog in the middle of current parent dialog
        int x, y;
        int thisx = this.getX(), thisy = this.getY();
        int thiswidth = this.getWidth(), thisheight = this.getHeight();
        x = thisx + ((thiswidth - width) / 2);
        y = thisy + ((thisheight - height) / 2);

        currentModifyingAttributeObj = attributeObj;
        oldAttributeName = _attributeName;
        jTestField_attributeName.setText(_attributeName);

        this.changeAttributeName.setLocation(x, y);
        this.changeAttributeName.setVisible(true);

    }
    private AttributeListObj currentModifyingAttributeObj;

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        String newname = jTestField_attributeName.getText();
        if ((newname == null) || (newname.trim().length() < 1)) {
            commons.Tools.beep();
            return;
        }

        this.changeAttributeName.setVisible(false);

        if (currentModifyingAttributeObj.isPublicAttribute()) {
            ArrayList values = env.Parameters.AttributeSchemas.getAllowed(newname);
            if ((values != null) && (values.size() > 0)) {
                commons.Tools.beep();
                return;
            }
        } else {
            resultEditor.annotationClasses.Depot classdepot = new resultEditor.annotationClasses.Depot();
            AnnotationClass ac = classdepot.getAnnotatedClass(Status.currentClass);
            if (ac != null) {
                if (ac.hasAttributeName(newname)) {
                    commons.Tools.beep();
                    return;
                }
            }
        }

        // save the modification we just did to the selected attribute item.
        saveChange(newname.trim());

        // update the list of attribute name
        this.toFront();
        this.requestFocus();
        commons.Tools.beep();
        
        config.project.ProjectConf projectconf = new config.project.ProjectConf(
                    env.Parameters.WorkSpace.CurrentProject);
        projectconf.saveConfigure();

    }//GEN-LAST:event_jButton2ActionPerformed

    /**Saving the modification we just did to the selected attribute item. This
     * method should be called after user clicked "Ok" button on the dialog
     * of changing attribute name.
     */
    private void saveChange(String _newAttributeName) {
        if (_newAttributeName == null) {
            return;
        }
        if (_newAttributeName.trim().length() < 1) {
            return;
        }

        currentModifyingAttributeObj.getAttribute().rename(_newAttributeName);
        if (currentModifyingAttributeObj.isPublicAttribute()) {
            listPublicAttributes();
        } else {
            this.listAttributesOf(Status.currentClass);
        }

        nameText.setText("");


    }

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        this.changeAttributeName.setVisible(false);
        this.toFront();
        this.requestFocus();
        commons.Tools.beep();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void list_of_classesMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_list_of_classesMouseReleased
        listAttributeNames();
    }//GEN-LAST:event_list_of_classesMouseReleased

    public void listAttributeNames(){
        
        jPanel_panelOfUMLSOptions.setVisible( false );
        
        int selectedIndex = this.list_of_classes.getSelectedIndex();
        Status.currentClass = null;

        // if no item is selected
        if (selectedIndex == -1) {
            return;
        }

        // if we selected the first item: the "public attributes"
        if (selectedIndex == 0) {
            this.listPublicAttributes();
            this.list_of_attributeNames.setDragEnabled(true);
            jPanel22.setVisible(false);
        } else {
            this.list_of_attributeNames.setDragEnabled(false);
            Object selectedObj = this.list_of_classes.getModel().getElementAt(selectedIndex);
            ClassObj co = (ClassObj) selectedObj;
            String classname = co.getClassName();
            Status.currentClass = classname; // record current class
            this.listAttributesOf(classname);
            jPanel22.setVisible(true);
            if (co.getInherit()) {
                jCheckBox1.setSelected(true);
            } else {
                jCheckBox1.setSelected(false);
            }
        }
    }
    
    
    private void upActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_upActionPerformed
        if (this.list_of_attributeNames.getSelectedIndex() == -1) {
            return;
        }

        if (this.list_of_attributeNames.getModel().getSize() < 1) {
            return;
        }

        try {
            AttributeListObj ao = (AttributeListObj) list_of_attributeNames.getSelectedValue();
            int selectedIndex = this.list_of_attributeNames.getSelectedIndex();
            int size = this.list_of_attributeNames.getModel().getSize();

            if (selectedIndex == 0) {
                up.setEnabled(false);
                return;
            }

            String classname = Status.currentClass;
            resultEditor.annotationClasses.Depot classDepot =
                    new resultEditor.annotationClasses.Depot();
            AnnotationClass ac = classDepot.getAnnotatedClass(classname);
            if (ac != null) {
                ac.moveUp_attribute(selectedIndex);
            }

            this.listAttributesOf(classname);
            this.list_of_attributeNames.setSelectedIndex(selectedIndex - 1);


            if (selectedIndex - 1 == 0) {
                up.setEnabled(false);
            } else {
                up.setEnabled(true);
            }


        } catch (Exception ex) {
        }

    }//GEN-LAST:event_upActionPerformed

    /**move down*/
    private void downActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downActionPerformed

        if (this.list_of_attributeNames.getSelectedIndex() == -1) {
            return;
        }
        if (this.list_of_attributeNames.getModel().getSize() < 1) {
            return;
        }

        try {
            AttributeListObj ao = (AttributeListObj) list_of_attributeNames.getSelectedValue();
            int selectedIndex = this.list_of_attributeNames.getSelectedIndex();
            int size = this.list_of_attributeNames.getModel().getSize();

            if (selectedIndex == size - 1) {
                down.setEnabled(false);
                return;
            }

            String classname = Status.currentClass;
            resultEditor.annotationClasses.Depot classDepot =
                    new resultEditor.annotationClasses.Depot();
            AnnotationClass ac = classDepot.getAnnotatedClass(classname);
            if (ac != null) {
                ac.moveDown_attribute(selectedIndex);
            }

            this.listAttributesOf(classname);
            this.list_of_attributeNames.setSelectedIndex(selectedIndex + 1);


            if (selectedIndex + 1 >= size - 1) {
                down.setEnabled(false);
            } else {
                down.setEnabled(true);
            }


        } catch (Exception ex) {
        }        // TODO add your handling code here:
    }//GEN-LAST:event_downActionPerformed

    private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
        if (this.list_of_classes.getSelectedIndex() == -1) {
            return;
        }

        Object selectedObj = this.list_of_classes.getSelectedValue();
        if (selectedObj == null) {
            return;
        }



        if (selectedObj instanceof ClassObj) {
            ClassObj co = (ClassObj) selectedObj;
            co.setInherit(this.jCheckBox1.isSelected());

            resultEditor.annotationClasses.Depot classdepot =
                    new resultEditor.annotationClasses.Depot();
            AnnotationClass ac = classdepot.getAnnotatedClass(co.getClassName());
            if (ac != null) {
                ac.inheritsPublicAttributes = this.jCheckBox1.isSelected();
            }
        }
    }//GEN-LAST:event_jCheckBox1ActionPerformed

    private void upButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_upButton2ActionPerformed
        moveup_down(true);
    }//GEN-LAST:event_upButton2ActionPerformed

    /**move a attrinute up*/
    private void moveup_down(boolean isMoveUp) {

        int selectedIndex = list_of_attributeValues.getSelectedIndex();
        if (selectedIndex == -1) {
            return;
        }

        if (isMoveUp) {
            if (selectedIndex == 0) {
                upButton2.setEnabled(false);
                return;
            }
        } else {
            if (selectedIndex >= list_of_attributeValues.getModel().getSize() - 1) {
                downButton2.setEnabled(false);
                return;
            }
        }

        AttributeListObj ao;
        try {
            ao = (AttributeListObj) this.list_of_attributeNames.getSelectedValue();
        } catch (Exception ex) {
            ao = null;
        }

        if (ao == null) {
            return;
        }

        String temp = ao.getAttribute().getAllowedEntries().get(selectedIndex);

        if (isMoveUp) {
            String previous = ao.getAttribute().getAllowedEntries().get(selectedIndex - 1);
            ao.getAttribute().getAllowedEntries().set(selectedIndex - 1, temp);
            ao.getAttribute().getAllowedEntries().set(selectedIndex, previous);
        } else {
            String next = ao.getAttribute().getAllowedEntries().get(selectedIndex + 1);
            ao.getAttribute().getAllowedEntries().set(selectedIndex + 1, temp);
            ao.getAttribute().getAllowedEntries().set(selectedIndex, next);
        }

        // refresh screen after modification
        this.listAttributeValues(ao.getAttribute(), ao.isPublicAttribute());

        if (isMoveUp) {
            selectedIndex = selectedIndex - 1;
        } else {
            selectedIndex = selectedIndex + 1;
        }

        this.list_of_attributeValues.setSelectedIndex(selectedIndex);

        if (selectedIndex <= 0) {
            upButton2.setEnabled(false);
        } else {
            upButton2.setEnabled(true);
        }

        if (selectedIndex >= this.list_of_attributeValues.getModel().getSize() - 1) {
            downButton2.setEnabled(false);
        } else {
            downButton2.setEnabled(true);
        }

    }

    private void downButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downButton2ActionPerformed
        moveup_down(false);
    }//GEN-LAST:event_downButton2ActionPerformed

    private void sortButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sortButton2ActionPerformed
        sortValues();
    }//GEN-LAST:event_sortButton2ActionPerformed

    private void sortValues() {
        AttributeListObj ao;
        try {
            ao = (AttributeListObj) this.list_of_attributeNames.getSelectedValue();
        } catch (Exception ex) {
            ao = null;
        }

        if (ao == null) {
            return;
        }

        try {
            // sort current attribute values
            Vector<String> sortedAttributeValuess = this.sort(ao.getAttribute().getAllowedEntries());

            //
            int selectedIndex = this.list_of_attributeNames.getSelectedIndex();

            if (selectedIndex != -1) {
                if (ao.isPublicAttribute()) {

                    //Vector<Attribute> allAttributes = env.Parameters.SimpleRelationshipNames.getAttributes();
                    Status.currentAttribute.setAllowedEntries(sortedAttributeValuess);
                    this.listAttributeValues(Status.currentAttribute, true);

                } else {

                    resultEditor.annotationClasses.Depot classdepot =
                            new resultEditor.annotationClasses.Depot();
                    AnnotationClass ac = classdepot.getAnnotatedClass(Status.currentClass);
                    ac.privateAttributes.setElementAt(new AttributeSchemaDef(ao.getAttributeName(), sortedAttributeValuess), selectedIndex);
                    this.listAttributeValues(ao.getAttribute(), false);
                }
            }

            

            

        } catch (Exception ex) {
        }

        this.upButton2.setEnabled(false);
        this.downButton2.setEnabled(false);
    }
    private void sortButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sortButton1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_sortButton1ActionPerformed

    
        
    
    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        
        config.project.ProjectConf projectconf = new config.project.ProjectConf(
                    env.Parameters.WorkSpace.CurrentProject);
        projectconf.saveConfigure();
        
        this.gui.setEnabled( true );
    }//GEN-LAST:event_formWindowClosing

    
    private void formWindowDeactivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowDeactivated
        if((changeAttributeName!=null)&&(changeAttributeName.isVisible()))
            return;
        this.toFront();
        this.requestFocus();
        commons.Tools.beep();
    }//GEN-LAST:event_formWindowDeactivated

    private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox2ActionPerformed
        
        // get current selected attribute schema
        if( list_of_attributeNames.getSelectedIndex() < 0 )
            return;
        if( this.list_of_attributeValues.getSelectedIndex() < 0 )
            return;
        AttributeListObj att = (AttributeListObj) this.list_of_attributeNames.getSelectedValue();
        AttributeValueObj value = (AttributeValueObj) this.list_of_attributeValues.getSelectedValue();
        
        if( jCheckBox2.isSelected() )
            att.getAttribute().setDefaultValue( value.getAttributeValue() );
        
        else if( !jCheckBox2.isSelected()  ){
            att.getAttribute().removeDefaultValue();
        }
        
        int index1 = list_of_attributeNames.getSelectedIndex();
        int index2 = list_of_attributeValues.getSelectedIndex();        
        listAttributeNames();
        list_of_attributeNames.setSelectedIndex( index1 );
        list_of_attributeValues.setSelectedIndex( index2 );
        
        
        config.project.ProjectConf projectconf = new config.project.ProjectConf(
                    env.Parameters.WorkSpace.CurrentProject);
        projectconf.saveConfigure();
                    
        
        
                
    }//GEN-LAST:event_jCheckBox2ActionPerformed

    /**change the UMLS status of current selected attribute.*/
    private void jCheckBox_linkCUICodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox_linkCUICodeActionPerformed
        
        // get current attribute
        AttributeListObj ao = null;
        try {
            ao = (AttributeListObj) this.list_of_attributeNames.getSelectedValue();
        } catch (Exception ex) {
            ao = null;
        }

        if (ao == null) {
            return;
        }

        // set the UMLS coding option
        ao.setUMLSLinkOptions( jCheckBox_linkCUICode.isSelected(), jCheckBox_linkCUILABEL.isSelected() );

        int selected = list_of_attributeNames.getSelectedIndex();
        
        listAttributeNames();
        
        list_of_attributeNames.setSelectedIndex( selected );
        /*
        String text = this.valueText.getText();
        for ( String existingValue : Status.currentAttribute.getAllAllowedEntries() ){
            if( existingValue == null )
                continue;
            if ( existingValue.compareTo( text.trim() ) == 0 ){
                valueText.setText("");
                return;
            }
        }
        * */
        
        config.project.ProjectConf projectconf = new config.project.ProjectConf(
                    env.Parameters.WorkSpace.CurrentProject);
        projectconf.saveConfigure();
        
        //Status.currentAttribute.getAllAllowedEntries().add(text);
    }//GEN-LAST:event_jCheckBox_linkCUICodeActionPerformed

    private void jCheckBox_linkCUILABELActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox_linkCUILABELActionPerformed
        // get current attribute
        AttributeListObj ao = null;
        try {
            ao = (AttributeListObj) this.list_of_attributeNames.getSelectedValue();
        } catch (Exception ex) {
            ao = null;
        }

        if (ao == null) {
            return;
        }

        // set the UMLS linking options for this attributes
        ao.setUMLSLinkOptions( jCheckBox_linkCUICode.isSelected(), jCheckBox_linkCUILABEL.isSelected() );

        int selected = list_of_attributeNames.getSelectedIndex();
        
        listAttributeNames();
        
        list_of_attributeNames.setSelectedIndex( selected );
    }//GEN-LAST:event_jCheckBox_linkCUILABELActionPerformed
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Private Methods">
    /**put this dialog in the middle of eHOST's main window*/
    private void setDialogLocation() {
        //If null was passed into the constructor then just return
        if (gui == null) {
            return;
        }

        //If non null gui then center the gui in the Callers window.
        int parentX = gui.getX(), parentY = gui.getY();
        int parentWidth = gui.getWidth(), parentHeight = gui.getHeight();
        int width = this.getWidth(), height = this.getHeight();

        int x = parentX + (int) ((parentWidth - width) / 2);
        int y = parentY + (int) ((parentHeight - height) / 2);

        this.setLocation(x, y);
        
        // hide the panel that contains checkbox of UMLS options 
        // while we just open the dialog.
        jPanel_panelOfUMLSOptions.setVisible( false );
    }

    /**
     * Update the list of relationship names
     */
    private void listPublicAttributes() {
        Status.currentClass = "PUBLIC-ATTRIBUTES";

        list_of_attributeNames.setListData(new Vector());

        Vector<AttributeListObj> listInfo = new Vector<AttributeListObj>();
        for (AttributeSchemaDef att : env.Parameters.AttributeSchemas.getAttributes()) {
            AttributeListObj ao = new AttributeListObj(att.getName(), att, this.iconTable.get("attribute"), true);
            listInfo.add(ao);
        }
        list_of_attributeNames.setListData(listInfo);
        list_of_attributeNames.setCellRenderer(new AttributeRenderer());
    }

    private void listAttributesOf(String classname) {

        // empty the list before refresh
        list_of_attributeNames.setListData(new Vector());

        // reloading
        Vector<AttributeListObj> listInfo = new Vector<AttributeListObj>();
        resultEditor.annotationClasses.Depot classDepot = new resultEditor.annotationClasses.Depot();
        AnnotationClass annotationClass = classDepot.getAnnotatedClass(classname);
        Vector<AttributeSchemaDef> attributes = annotationClass.getAttributes();
        for (AttributeSchemaDef att : attributes) {
            AttributeListObj ao = new AttributeListObj(att.getName(), att, this.iconTable.get("attribute"), false );
            listInfo.add(ao);
        }
        list_of_attributeNames.setListData(listInfo);
        list_of_attributeNames.setCellRenderer(new AttributeRenderer());
    }

    /**
     * Update the list of allowable values based on the passed in attribute.
     * @param value - the attribute to display the allowed values
     */
    private void listAttributeValues(AttributeSchemaDef attributeSchema, boolean isPublicAttribute) {
        list_of_attributeValues.setListData(new Vector());

        if (attributeSchema == null) {
            return;
        }
        Vector<AttributeValueObj> listData = new Vector<AttributeValueObj>();
        for (String value_str : attributeSchema.getAllowedEntries()) {
            AttributeValueObj avo = new AttributeValueObj(value_str,
                    this.iconTable.get("attributevalue"), 
                    isPublicAttribute, attributeSchema);
            listData.add(avo);
        }
        list_of_attributeValues.setListData(listData);
        list_of_attributeValues.setCellRenderer(new AttributeValueRenderer());
    }

    /**
     * This method will add a new relationship using the text in the textbox.
     */
    private void addNewRelationshipFromTextBox() {
        if (Status.currentClass == null) {
            return;
        }
        if ((nameText.getText() == null) || (nameText.getText().trim().length() < 1)) {
            return;
        }

        if (Status.currentClass.compareTo("PUBLIC-ATTRIBUTES") == 0) {
            AttributeSchemaDef toAdd = new AttributeSchemaDef(nameText.getText(), new Vector<String>());

            boolean reallyNew = true;
            //Don't allow duplicates
            for (AttributeSchemaDef existing : env.Parameters.AttributeSchemas.getAttributes()) {
                if (existing.getName().equals(toAdd.getName())) {
                    reallyNew = false;
                }
            }
            if (reallyNew) {
                env.Parameters.AttributeSchemas.Add(toAdd);
            }
            listPublicAttributes();
            list_of_attributeValues.setSelectedValue(toAdd, true);
            nameText.setText("");
        } else {
            resultEditor.annotationClasses.Depot classDepot = new resultEditor.annotationClasses.Depot();
            AnnotationClass ac = classDepot.getAnnotatedClass(Status.currentClass);
            String attribtuename = nameText.getText().trim();
            if(env.Parameters.AttributeSchemas.getAttribute(attribtuename) != null )
            {
                commons.Tools.beep();
                return;
            }
                
            ac.putAttribute(nameText.getText(), new Vector<String>());
            this.listAttributesOf(Status.currentClass);

        }
    }

    /**
     * This method will add a new allowable value using the text in the textbox.
     */
    private void addNewAttributeValue() {

        if( valueText.getText() == null )
            return;
        if( valueText.getText().trim().length() < 1 )
            return;

        if (this.list_of_attributeNames.getSelectedIndex() == -1) {
            return;
        }

        // get current attribute
        AttributeListObj ao = null;
        try {
            ao = (AttributeListObj) this.list_of_attributeNames.getSelectedValue();
        } catch (Exception ex) {
            ao = null;
        }

        if (ao == null) {
            return;
        }



        String text = this.valueText.getText();
        for ( String existingValue : Status.currentAttribute.getAllAllowedEntries() ){
            if( existingValue == null )
                continue;
            if ( existingValue.compareTo( text.trim() ) == 0 ){
                commons.Tools.beep();
                valueText.setText("");
                return;
            }
        }
        //Status.currentAttribute.getAllAllowedEntries().add(text);
        Status.currentAttribute.put(text);

        listAttributeValues(ao.getAttribute(), ao.isPublicAttribute());
        // list_of_attributeNames.setSelectedValue(text, true);
        valueText.setText("");
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="GUI Member Variables">
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addName;
    private javax.swing.JButton addValue;
    private javax.swing.JDialog changeAttributeName;
    private javax.swing.JButton deleteButton2;
    private javax.swing.JButton down;
    private javax.swing.JButton downButton2;
    private javax.swing.JLabel icon_1;
    private javax.swing.JLabel icon_2;
    private javax.swing.JLabel icon_3;
    private javax.swing.JLabel icon_4;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton4;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBox_linkCUICode;
    private javax.swing.JCheckBox jCheckBox_linkCUILABEL;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JPanel jPanel14;
    private javax.swing.JPanel jPanel15;
    private javax.swing.JPanel jPanel16;
    private javax.swing.JPanel jPanel17;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel21;
    private javax.swing.JPanel jPanel22;
    private javax.swing.JPanel jPanel23;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JPanel jPanel_panelOfUMLSOptions;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextField jTestField_attributeName;
    private javax.swing.JList list_of_attributeNames;
    private javax.swing.JList list_of_attributeValues;
    private javax.swing.JList list_of_classes;
    private javax.swing.JTextField nameText;
    private javax.swing.JButton removeName;
    private javax.swing.JButton sortButton1;
    private javax.swing.JButton sortButton2;
    private javax.swing.JButton up;
    private javax.swing.JButton upButton2;
    private javax.swing.JTextField valueText;
    // End of variables declaration//GEN-END:variables
    //</editor-fold>

    /**Show "PUBLIC" item and all classnames in the list.*/
    private void showClasses() {
        // empty the list before update
        Vector<ClassObj> listData = new Vector<ClassObj>();

        list_of_classes.setListData(listData);

        ClassObj pub = new ClassObj("public", this.iconTable.get("public"), false);
        listData.add(pub);

        String[] classnames = getAllClasses();
        if (classnames == null) {
            return;
        }

        resultEditor.annotationClasses.Depot classdepot =
                new resultEditor.annotationClasses.Depot();


        for (String classname : classnames) {
            AnnotationClass ac = classdepot.getAnnotatedClass(classname);
            boolean flag = true;
            if (ac != null) {
                flag = ac.inheritsPublicAttributes;
            }
            ClassObj co = new ClassObj(
                    classname, this.iconTable.get("class"), flag);
            listData.add(co);
        }

        list_of_classes.setCellRenderer(new ClassRenderer());
        list_of_classes.setListData(listData);

    }

    /**Get names of all classes*/
    private String[] getAllClasses() {
        resultEditor.annotationClasses.Depot classDepot = new resultEditor.annotationClasses.Depot();
        return classDepot.getAnnotationClasssnamesString();
    }

    /**this class contains several variables that indicate currrent class,
     * current attribute name, etc*/
    static class Status {

        /**The name of the current selected class, notice: if there is */
        public static String currentClass = null;
        /**The name of the current selected attribute*/
        public static AttributeSchemaDef currentAttribute = null;
    }

    private Vector<String> sort(Vector<String> strings) throws Exception {
        if (strings == null) {
            throw new Exception("1201061612:: we could not sort an empty list.");
        }

        if (strings.size() <= 1) {
            throw new Exception("1201061613:: we could not sort an empty list.");
        }

        int size = strings.size();

        for (int i = 0; i < size; i++) {
            for (int j = i+1; j < size; j++) {
                
                String first = strings.get(i);
                String second = strings.get(j);

                // if string 2 is smaller than string 1 in an alphe-number
                // sequency, then we will move the smaller string (second string)
                // to front, then we can keep the position of string 1 always
                // be small in the string array.
                if (compareTwoStrings( first, second ) == true ) {
                    
                    strings.setElementAt(first, j);
                    strings.setElementAt(second, i);
                }
            }
        }

        return strings;
    }

    /**compare two strings. Return true if second string has low sequency chars
     * than the first string in same position; or first string is longer while
     * the common part of them are same.
     *
     * ##return true if second is smaller.##
     */
    private boolean compareTwoStrings(String str1, String str2) throws Exception {
        if ((str1 == null) || (str2 == null)) {
            throw new Exception("1201061537::can not compare empty strings!");
        }

        char[] chars1 = str1.toLowerCase().toCharArray();
        char[] chars2 = str2.toLowerCase().toCharArray();

        int size1 = chars1.length;
        int size2 = chars2.length;

        int size;
        if (size1 > size2) {
            size = size2;
        } else {
            size = size1;
        }

        for (int i = 0; i < size; i++) {

            int i1 = Integer.valueOf( chars1[i] );
            int i2 = Integer.valueOf( chars2[i] );

            if ( i1 > i2 ) {
                return true;
            }else if( i1 < i2 )
                return false;
        }

        if (size1 > size2) {
            return true;
        }

        return false;
    }
}


